import { request } from '../../assets/js/request.js';
import Notify from '@vant/weapp/notify/notify';
Page({
  _getInitInfo: function(){
    wx.showLoading({
      title: '加载数据',
      mask: true,
    })
    request('/getSignInit.php', { classId: wx.getStorageSync('classId') })
      .then((res)=>{
        if (res.data.isSign == 1) {
          this.setData({
            isSign: true,
            btnText: '结束签到',
            signPwd: res.data.signPwd,
            signLocation: res.data.signLocation
          })
        } else {
          this.setData({
            isSign: false,
            btnText: '开始签到'
          })
        }
      })
      .catch((err)=>{
        Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
      })
  },

  toggleSignState: function (e) {
    if (this.data.isSign) {
      wx.showLoading({
        title: '请稍后',
        mask: true,
      })
      request('/closeSign.php', { classId: wx.getStorageSync('classId')})
        .then((res)=>{
          if (res.data == 'close') {
            Notify({ type: 'success', message: '签到已结束' });
            this.setData({
              isSign: false,
              btnText: '开始签到',
              signPwd: '',
              signLocation: ''
            })
          } else if (res.data == 'fail') {
            Notify({ type: 'danger', message: '请求失败，请稍后再试' });
          }
        })
        .catch((err)=>{
          Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
        })
    } else {
      if(this.data.signLocation == ""){
        Notify({ type: 'danger', message: '请先选择签到位置' });
      }else{
        wx.showLoading({
          title: '请稍后',
          mask: true,
        })
        let thisData = {
          classId: wx.getStorageSync('classId'),
          signLocation: this.data.signLocation
        }
        request('/startSign.php',thisData)
          .then((res)=>{
            if (res.data.state == 'start') {
              Notify({ type: 'success', message: '签到已开始' });
              this.setData({
                isSign: true,
                btnText: '结束签到',
                signPwd: res.data.signPwd
              })
            } else if (res.data.state == 'fail') {
              Notify({ type: 'danger', message: '请求失败，请稍后再试' });
            }
          })
          .catch((err)=>{
            Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
          })
      }
    }
  },

  _chooseLocation: function(e){
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: (res) => {
              if (res.confirm) {
                wx.openSetting({
                  success:(res)=>{
                    
                  }
                })
              }
            }
          })
        }else{
          wx.showLoading({
            title: '正在打开地图',
            mask: true,
          })
          wx.chooseLocation({
            success: (res) => {
              console.log(res)
              this.setData({
                signLocation: res.latitude + ',' + res.longitude,
              })
            },
            complete: (res) => {
              wx.hideLoading();
            }
          })
        }
      },
    })
    
  },

  handlerCopy: function (e) {
    if (this.data.signPwd != ''){
      wx.setClipboardData({
        data: this.data.signPwd,
        success: (res) => {
          wx.getClipboardData({})
        }
      })
    }
  },

  data: {
    isSign: false,
    btnText: '开始签到',
    signLocation: '',
    signPwd:''
  },

  onLoad: function (options) {

  },

  onShow: function () {
    this._getInitInfo();
  },

  onPullDownRefresh: function () {

  },

})